package net.zoneland.data.dataprofiling.common.service.impl;

import net.zoneland.data.dataprofiling.common.domain.DictEntity;
import net.zoneland.data.dataprofiling.common.domain.DictQuery;
import net.zoneland.data.dataprofiling.common.repository.DictRepository;
import net.zoneland.data.dataprofiling.common.service.DictService;
import net.zoneland.data.dataprofiling.common.utils.PageUtils;
import net.zoneland.data.dataprofiling.common.utils.StringUtils;
import net.zoneland.data.dataprofiling.system.domain.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictRepository dictRepository;

    @Override
    public DictEntity get(Long id) {
        return dictRepository.getOne(id);
    }

    @Override
    public PageUtils list(Integer page, Integer size, DictQuery dictQuery) {
        Pageable pageable = PageRequest.of(page,size);
        Page<DictEntity> dictEntityPage = dictRepository.findAll(new DictSpecification(dictQuery),pageable);
        PageUtils pageUtils = new PageUtils(dictEntityPage.getContent(), (int) dictEntityPage.getTotalElements());
        return pageUtils;
    }

    @Override
    public int count(DictQuery dictQuery) {
        return 0;
    }


    @Override
    public int save(DictEntity dict) {
        if(dictRepository.save(dict)!=null){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int update(DictEntity dict) {
        DictEntity d = dictRepository.saveAndFlush(dict);
        if(d!=null){
            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public int remove(Long id) {
      dictRepository.deleteById(id);
      return 0;
    }

    @Override
    public int batchRemove(Long[] ids) {
        for(Long id:ids){
            dictRepository.deleteById(id);
        }
        return 0;
    }

    @Override
    public List<DictEntity> listType() {
        return dictRepository.findDistinctTypeAndDescription();
    }

    @Override
    public String getName(String type, String value) {
        List<DictEntity> dicts = dictRepository.findByTypeAndValue(type,value);
        if(dicts!=null && !dicts.isEmpty()){
            return dicts.get(0).getName();
        }else {
            return "";
        }
    }

    @Override
    public List<DictEntity> getHobbyList(UserEntity userDO) {
        List<DictEntity> hobbyList = dictRepository.findByType("hobby");
        if(StringUtils.isNotEmpty(userDO.getHobby())){
            String userHobbys[] = userDO.getHobby().split(";");
            for (String userHobby : userHobbys) {
                for (DictEntity hobby : hobbyList) {
                    if (!Objects.equals(userHobby, hobby.getId().toString())) {
                        continue;
                    }
                    hobby.setRemarks("true");
                    break;
                }
            }
        }
        return hobbyList;
    }

    @Override
    public List<DictEntity> getSexList() {
        return dictRepository.findByType("sex");
    }

    @Override
    public List<DictEntity> listByType(String type) {
        return dictRepository.findByType(type);
    }
}
